package com.uchengwang.ui;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.uchengwang.ui.base_ui.ui.MainActivity;
import com.uchengwang.ui.cm_ui.rychat.msgtype.BigEmoMessage;
import com.uchengwang.ui.cm_ui.rychat.msgtype.BigEmotionItemProvider;
import com.uchengwang.ui.cm_ui.rychat.msgtype.TextMsgItemProvider;
import com.uchengwang.ui.cm_ui.rychat.msgtype.UcItemProvider;
import com.uchengwang.ui.cm_ui.rychat.msgtype.UcTypeMessage;
import com.uchengwang.ui.cm_ui.rychat.utils.SealAppContext;
import com.uchengwang.ui.cm_ui.umsg.ui.ClubMainActivity;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import io.rong.imkit.RongIM;
import okhttp3.OkHttpClient;

/**
 * Created by U城网 on 2017/3/3.
 */

public class UChengApplication extends Application{
    private static OkHttpClient okHttpClient;
    private static Context mContext;
    private List<Activity> mList = new ArrayList<>();
    public static UChengApplication instance;
    public static boolean isFormalNet = false;//true是正式环境  false是测试环境

    public static boolean isAccountChange = false;//是否切换了账号/是否认证了/是否切换了学校

    public static String schoolName = "";//切换学校名称
    public static String mySchoolName = "";//我的认证学校

    public static long uniId = 0;//学校Id 切换学校时用, 用于获取

    public static long myUniId = 0;//自己所在的学校ID, 发布信息时用
    public static long uniDataId;//班级唯一的ID

    public static int sex;//0=女 1=男 其他 未设置
    public static int chatType =0;//聊天类型 0普通聊天 1享聊类型

    public static long uid = 0;//个人U城网账号
    public static long clubId =0;//社团ID
    public static int userRole;//当前身份 1普通学生 2社团 3 uni
    public static String nickName = "";
    public static String TOKEN  = "";//token
    public static String userAvatar = "";//用户头像
    public static boolean isAuthen = false;//用户是否已在U城网认证
    public static boolean isCurrentLinkAccount = false;//当前是不是关联账号 true是 false不是 是正常账号
    public static boolean isCycleDelete = false;//是否h5界面删除了信息 返回刷新
    public WeakReference<Activity> appActivity;

//    public static UChengApplication getInstance() {
////        if (instance == null) {
////            instance = new UChengApplication();
////        }
//        return instance;
//    }



    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush
        mContext = getApplicationContext();

        MobclickAgent.setDebugMode( true );
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);//友盟统计设置场景
        MobclickAgent.enableEncrypt(true);//6.0.0版本及以后 日志加密设置
        MobclickAgent.openActivityDurationTrack(false);//禁止默认的页面统计方式，这样将不会再自动统计Activity。

        RongIM.init(this);//融云初始化
        Log.d("ryjjf", "在这里融云初始化了");
        SealAppContext.init(this);//融云
        //自定义表情
        RongIM.registerMessageType(BigEmoMessage.class);
        RongIM.registerMessageTemplate(new BigEmotionItemProvider());
        RongIM.registerMessageTemplate(new TextMsgItemProvider());

        //发送自定义U城信息
        RongIM.registerMessageType(UcTypeMessage.class);
        RongIM.registerMessageTemplate(new UcItemProvider());

        this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
                if(appActivity == null && activity instanceof MainActivity){
                    UChengApplication.instance.appActivity = new WeakReference<Activity>(activity);
                }
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                Log.d("msg", "onActivityCreated=name="+activity.getComponentName());
                if (activity instanceof MainActivity || activity instanceof ClubMainActivity){
                    Log.d("msg", "onActivityCreated=啦啦啦啦啦");
                    UChengApplication.instance.appActivity = new WeakReference<Activity>(activity);
                }
            }
        });

//        setupLeakCanary();


        Log.d("jjf", "appliactoinappliactoin地址="+UChengApplication.instance);



    }

//    private RefWatcher refWatcher;
//    private RefWatcher setupLeakCanary() {
//        if (LeakCanary.isInAnalyzerProcess(this)) {
//            return RefWatcher.DISABLED;
//        }
//        refWatcher = LeakCanary.install(this);
//        return refWatcher;
//    }

//    public static RefWatcher getRefWatcher(Context context) {
//        UChengApplication leakApplication = (UChengApplication) context.getApplicationContext();
//        return leakApplication.refWatcher;
//    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public static OkHttpClient getHttpClient(){
        if (okHttpClient == null){
            okHttpClient = new OkHttpClient();
        }
        return okHttpClient;
    }

    public static Context getContext(){
        return mContext;
    }

    @Override
    public void onTerminate() {
        // 程序终止的时候执行
        super.onTerminate();

        UChengApplication.TOKEN = "";
        UChengApplication.uid = 0;
        UChengApplication.nickName = "";
        UChengApplication.uniId = 0;
        UChengApplication.myUniId = 0;
        UChengApplication.schoolName = "";
        UChengApplication.userAvatar = "";
        UChengApplication.userRole = -1;

        Log.d("jjf", "onTerminate");
    }

    // add Activity
    public void addActivity(Activity activity) {
        if (mList != null && !mList.contains(activity)){
            mList.add(activity);
        }
    }

    public List<Activity> getList(){
        return mList;
    }

}
